package com.zhihuichongcheng.application.ui.approval.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.DateUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.approval.ApprovalWaitBean;
import com.zhihuichongcheng.application.bean.approval.UserFormInfo;
import com.zhihuichongcheng.application.bean.mine.UserBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 3water on 2016/11/10.
 */

public class WaittingApprovalAdapter extends RecyclerView.Adapter<WaittingApprovalAdapter.ViewHold> {

    private OnItemOnclick<ApprovalWaitBean> onItemOnclick;
    private Context mContext;
    private List<ApprovalWaitBean> mListData = new ArrayList<>();

    public WaittingApprovalAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_waitting_approval, null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {
        final ApprovalWaitBean bean = mListData.get(position);
        UserFormInfo info = bean.getUserFormInfo();
        String title = bean.getTitle();
        if (!TextUtils.isEmpty(title)) {
            holder.mTvTitle.setText(title);
        } else {
            holder.mTvTitle.setText("");
        }
        holder.mTvFormId.setText(bean.getUserFormInstanceId());
        String createTime = bean.getCreateTime();
        Date date = DateUtil.parseStringToDate(createTime, "yyyy年MM月dd日 HH:mm:ss");
        holder.mTvTime.setText(DateUtil.formatDateToString(date, "yyyy-MM-dd HH:mm"));
        UserBean userBean = bean.getUser();
        holder.mTvName.setText(userBean.getUserName());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemOnclick != null) {
                    onItemOnclick.onClick(bean, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void setOnItemOnclick(OnItemOnclick<ApprovalWaitBean> onItemOnclick) {
        this.onItemOnclick = onItemOnclick;
    }

    public void clear() {
        mListData.clear();
    }

    public void setdata(List<ApprovalWaitBean> list) {
        this.mListData.addAll(list);
    }

    class ViewHold extends RecyclerView.ViewHolder {
        private TextView mTvTitle, mTvName, mTvTime, mTvFormId;
        private View view;

        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            mTvFormId = (TextView) itemView.findViewById(R.id.tv_form_id);
            mTvName = (TextView) itemView.findViewById(R.id.tv_name);
            mTvTime = (TextView) itemView.findViewById(R.id.tv_time);
        }
    }
}
